package com.xujianlun.cpms.controller.sys;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xujianlun.cpms.common.Constants;
import com.xujianlun.cpms.common.Response;
import com.xujianlun.cpms.entity.sys.Dict;
import com.xujianlun.cpms.mapper.sys.DictMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xujianlun.cpms.service.sys.IMenuService;
import com.xujianlun.cpms.entity.sys.Menu;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author xujianlun
 * @since 2022-11-24
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private IMenuService menuService;
    @Resource
    private DictMapper dictMapper;



    // 新增或者更新
    @PostMapping
    public Response save(@RequestBody Menu menu) {

        return Response.success(menuService.saveOrUpdate(menu));
    }

    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Integer id) {

        return Response.success(menuService.removeById(id));
    }

    @PostMapping("/batch")
    public Response deleteBatch(@RequestBody List<Integer> ids) {

        return Response.success(menuService.removeByIds(ids));
    }

    @GetMapping
    public Response findAll(@RequestParam(defaultValue = "") String name) {
        return Response.success(menuService.findAllMenus(name));
    }

    @GetMapping("/{id}")
    public Response findOne(@PathVariable Integer id) {
        return Response.success(menuService.getById(id));
    }

    @GetMapping("/ids")
    public Response findAllIds() {
        return Response.success(menuService.list().stream().map(Menu::getId));
    }

    @GetMapping("/page")
    public Response findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        queryWrapper.orderByDesc("id");
        return Response.success(menuService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
    //菜单图标
    @GetMapping("/icons")
    public Response getIcon(){
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        return Response.success(dictMapper.selectList(dictQueryWrapper));
    }
}

